UIBornGift = Class("UIBornGift", UIComponentBase)

function UIBornGift:OnCreate(id, parent)
    self.activeId = id
    self.parent = parent

    self.banner = self:GetChild("banner")
    self.titlePic = self:GetChild("titlePic")
    self.countDown = self:GetChild("titleTime")
    self.titleTips = self:GetChild("titleTips")
    self.giftList = self:GetChild("giftList")
    self.ctrlItem = CtrlManager.GetCtrl(PanelNames.Item);
end

function UIBornGift:Refresh()
    logicMgr.CostManager.updateActiveOpenStatus(self.activeId);
    self.severData, self.itemData, self.modelTableData = logicMgr.CostManager.getCostData(self.activeId);
    logicMgr.CostManager.updateCostHelp(self, self:GetChild("help"), self.modelTableData);
    self.countDownNum = self.severData.endTime - HelperFunc.GetServerTime();
    self:RemoveTimeCall(self.OnCountDown);
    if self.severData.endTime == -1 then
        self.countDown.text = "永久开启";
    elseif self.countDownNum <= 0 then
        self.countDown.text = "活动已结束";
    else
        self:TimeCall(self.OnCountDown, 1000, -1);
        self.countDown.text = HelperFunc.GetSurpTimeStr(self.countDownNum);
    end
    self.titlePic.url = "ui://UIBornGift/"..self.modelTableData.f_TitlePic;
    self.banner.url = "ui://UIBornGift/"..self.modelTableData.f_Banner;
    self.titleTips.text = getLanguage(self.modelTableData.f_Tips, nil, "");

    self:RemoveAllTimeCall();
    self.objItems = {};
    self.itemTimes = {};
    self.timeCallId = {};
    self.giftList:RemoveChildrenToPool();
    for k, v in pairs(self.severData.items) do
        local tableData = self.itemData[tostring(v.subId)];
        local item = self.giftList:AddItemFromPool()
        self:updateItem(item, k, v, tableData);
    end
    if #self.severData.items == 0 then
        logicMgr.CostManager.removeOneActive(self.activeId);
    end
    self.parent:updateCostShowByTableOrJson(self.modelTableData.f_ResourceShow);
end

function UIBornGift:updateItem(itemObj, index, serverData, tableData)
    local mark = itemObj:GetChild("mark");
    local title = itemObj:GetChild("title");
    local time = itemObj:GetChild("time");
    local buy = itemObj:GetChild("buy");
    local buyNum = itemObj:GetChild("buyNum");
    local fristItem = itemObj:GetChild("fristItem");
    local itemList = itemObj:GetChild("itemList");
    --mark.url = "ui://UIBornGift/"..tableData.f_ConnerMark;
    title.text = getLanguage(tableData.f_CreateCharaterStoreName, nil, "");
    buyNum.text = "限购"..serverData.residue.."次";
    if tableData.f_Price == 0 then
        buy.title = "免费";
    else
        buy.title = "￥"..tableData.f_Price;
    end
    buy.onClick:Clear();
    buy.onClick:Add(function() self:ClickBuy(serverData.subId, tableData.f_ProductId, 1, tableData.f_Price); end);
    logicMgr.CostManager.RefreshItem(fristItem, decodeJsonStrToTabel(tableData.f_MajorCommodity)[1]);
    logicMgr.CostManager.RefreshItemList(itemList, decodeJsonStrToTabel(tableData.f_NomalCommodity), 4);
end

function UIBornGift:OnCountDown()
    self.countDownNum = self.countDownNum - 1;
    if self.countDownNum <= 0 then
        self.countDown.text = "活动已结束";
        return true;
    end
    self.countDown.text = HelperFunc.GetSurpTimeStr(self.countDownNum);
end

function UIBornGift:ClickCancel(sender)
    self.tipsController.selectedIndex = 0;
end

function UIBornGift:ClickBuy(id, payId, num, money)
    logicMgr.CostManager.getRewardByIdAndIndex(self.activeId, id, payId, num, money, self.onBuySuccess, self);
end

function UIBornGift:onBuySuccess()
    --UISysTips.AddMsg("购买成功");
end